package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.StealPhotoInfo;
import com.github.postsanf.yinian.bean.StealPhotoInfoList;
import com.github.postsanf.yinian.bean.StealYearString;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;

public class StealEachPhotosActivity extends BaseActivity implements View.OnClickListener {
    private SimpleDraweeView stealeachphotos_you_avatar;
    private SimpleDraweeView stealeachphotos_you_pic1;
    private SimpleDraweeView stealeachphotos_you_pic2;
    private SimpleDraweeView stealeachphotos_you_pic3;
    private SimpleDraweeView stealeachphotos_me_avatar;
    private ImageView stealeachphotos_me_pic1;
    private ImageView stealeachphotos_me_pic2;
    private ImageView stealeachphotos_me_pic3;
    private TextView stealeachphotos_you_name;
    private TextView stealeachphotos_me_name;
    private TextView stealeachphotos_you_tip;
    private TextView stealeachphotos_me_tip;
    private ImageView stealeachphotos_me_empty;
    private LinearLayout stealeachphotos_me_layout;
    private int beUserid;
    private int groupid;
    private String yourunseenPhotoStr;
    private String yourPhotoStr;
    private ArrayList<String> yourlist = new ArrayList();
    private ArrayList<String> melist = new ArrayList();
    private StealPhotoInfoList stealPhotoInfoList;
    public static ArrayList<StealYearString> photos = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stealeachphoto);
        beUserid = getIntent().getIntExtra("beUserid", 0);
        groupid = getIntent().getIntExtra("groupid", 0);
        stealPhotoInfoList = new StealPhotoInfoList();
        initView();
        registerBoradcastReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i("xjs", "onrrrrrrrr");
        initData();
    }

    private void initData() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        mParam.put("beUserid", String.valueOf(beUserid));
        mParam.put("groupid", String.valueOf(application.getCurGid()));
        String urlsto = getString(R.string.SLIVER_URL) + "ShowPeepInterface";
        StringRequest req = new StringRequest(urlsto)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "initData===>" + s.toString());
                        try {
                            JSONObject result = new JSONObject(s);
                            if (result.optInt("code") == 0) {
                                photos.clear();
                                stealPhotoInfoList.getStealPhotoInfoList().clear();
                                JSONObject jsonObject = result.optJSONArray("data").optJSONObject(0);
                                JSONObject yourInfo = jsonObject.optJSONObject("yourInfo");
                                JSONObject youruserInfo = yourInfo.optJSONObject("userInfo");
                                ImageOptHelper.showFrescoAvatar(Uri.parse(youruserInfo.optString("upic")), stealeachphotos_you_avatar, StealEachPhotosActivity.this);
                                stealeachphotos_you_name.setText(youruserInfo.optString("unickname"));
                                stealeachphotos_me_tip.setText("今天你看到的" + youruserInfo.optString("unickname") + "的照片");
                                //我未看的照片
                                JSONArray yourunseenPhoto = yourInfo.optJSONArray("unseenPhoto");
                                if (yourunseenPhoto != null)
                                    yourunseenPhotoStr = yourunseenPhoto.toString();
                                //我已经看过的照片
                                JSONArray yourphotoInfo = yourInfo.optJSONArray("photoInfo");
                                yourlist.clear();
                                if (yourphotoInfo != null) {
                                    yourPhotoStr = yourphotoInfo.toString();
                                    stealeachphotos_me_empty.setVisibility(View.GONE);
                                    stealeachphotos_me_layout.setVisibility(View.VISIBLE);
                                    for (int v = 0; v < yourphotoInfo.length(); v++) {
                                        yourlist.add(yourphotoInfo.optJSONObject(v).optString("photo"));
                                        StealPhotoInfo stealPhotoInfo = new StealPhotoInfo();
                                        stealPhotoInfo.setYear(yourphotoInfo.optJSONObject(v).optString("year"));
                                        stealPhotoInfo.setPhoto(yourphotoInfo.optJSONObject(v).optString("photo"));
                                        stealPhotoInfo.setPosition(yourphotoInfo.optJSONObject(v).optString("position"));
                                        stealPhotoInfoList.getStealPhotoInfoList().add(stealPhotoInfo);
                                    }
                                    if (yourphotoInfo.length() > 0)
                                        ImageOptHelper.showFrescoStatus(yourlist.get(0), stealeachphotos_me_pic1, StealEachPhotosActivity.this);
                                    if (yourphotoInfo.length() > 1)
                                        ImageOptHelper.showFrescoStatus(yourlist.get(1), stealeachphotos_me_pic2, StealEachPhotosActivity.this);
                                    if (yourphotoInfo.length() > 2)
                                        ImageOptHelper.showFrescoStatus(yourlist.get(2), stealeachphotos_me_pic3, StealEachPhotosActivity.this);
                                } else {
                                    stealeachphotos_me_layout.setVisibility(View.GONE);
                                    stealeachphotos_me_empty.setVisibility(View.VISIBLE);
                                }
                                JSONObject myInfo = jsonObject.optJSONObject("myInfo");
                                JSONObject meuserInfo = myInfo.optJSONObject("userInfo");
                                ImageOptHelper.showFrescoAvatar(Uri.parse(meuserInfo.optString("upic")), stealeachphotos_me_avatar, StealEachPhotosActivity.this);
                                stealeachphotos_me_name.setText(meuserInfo.optString("unickname"));
                                //他已经看过你的照片
                                JSONArray mephotoInfo = myInfo.optJSONArray("photoInfo");
                                melist.clear();
                                if (mephotoInfo != null) {
                                    stealeachphotos_you_tip.setText("他今天又看了你" + mephotoInfo.length() + "张照片");
                                    for (int v = 0; v < mephotoInfo.length(); v++) {
                                        melist.add(mephotoInfo.optJSONObject(v).optString("photo"));
                                    }
                                    if (mephotoInfo.length() > 0)
                                        ImageOptHelper.showFrescoStatus(Uri.parse(melist.get(0)), stealeachphotos_you_pic1, StealEachPhotosActivity.this);
                                    if (mephotoInfo.length() > 1)
                                        ImageOptHelper.showFrescoStatus(Uri.parse(melist.get(1)), stealeachphotos_you_pic2, StealEachPhotosActivity.this);
                                    if (mephotoInfo.length() > 2)
                                        ImageOptHelper.showFrescoStatus(Uri.parse(melist.get(2)), stealeachphotos_you_pic3, StealEachPhotosActivity.this);
                                } else {
                                    stealeachphotos_you_tip.setText("他今天又看了你0张照片");
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }



    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionupdateStealEachPhotos);
        registerReceiver(mListReceiver, myIntentFilter);
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        stealeachphotos_you_avatar = (SimpleDraweeView) findViewById(R.id.stealeachphotos_you_avatar);
        stealeachphotos_you_name = (TextView) findViewById(R.id.stealeachphotos_you_name);
        stealeachphotos_you_tip = (TextView) findViewById(R.id.stealeachphotos_you_tip);
        stealeachphotos_you_pic1 = (SimpleDraweeView) findViewById(R.id.stealeachphotos_you_pic1);
        stealeachphotos_you_pic2 = (SimpleDraweeView) findViewById(R.id.stealeachphotos_you_pic2);
        stealeachphotos_you_pic3 = (SimpleDraweeView) findViewById(R.id.stealeachphotos_you_pic3);
        stealeachphotos_me_avatar = (SimpleDraweeView) findViewById(R.id.stealeachphotos_me_avatar);
        stealeachphotos_me_name = (TextView) findViewById(R.id.stealeachphotos_me_name);
        stealeachphotos_me_tip = (TextView) findViewById(R.id.stealeachphotos_me_tip);
        stealeachphotos_me_empty = (ImageView) findViewById(R.id.stealeachphotos_me_empty);
        stealeachphotos_me_layout = (LinearLayout) findViewById(R.id.stealeachphotos_me_layout);
        stealeachphotos_me_pic1 = (ImageView) findViewById(R.id.stealeachphotos_me_pic1);
        stealeachphotos_me_pic2 = (ImageView) findViewById(R.id.stealeachphotos_me_pic2);
        stealeachphotos_me_pic3 = (ImageView) findViewById(R.id.stealeachphotos_me_pic3);
        stealeachphotos_me_avatar.setOnClickListener(this);
        stealeachphotos_you_pic1.setOnClickListener(this);
        stealeachphotos_you_pic2.setOnClickListener(this);
        stealeachphotos_you_pic3.setOnClickListener(this);
        stealeachphotos_me_pic1.setOnClickListener(this);
        stealeachphotos_me_pic2.setOnClickListener(this);
        stealeachphotos_me_pic3.setOnClickListener(this);
        stealeachphotos_me_empty.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.stealeachphotos_you_pic1:
                if (melist.size() > 0) {
                    Intent picintent1 = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent1.putExtra("list", melist);
                    picintent1.putExtra("poistion", 0);
                    startActivity(picintent1);
                }
                break;
            case R.id.stealeachphotos_you_pic2:
                if (melist.size() > 1) {
                    Intent picintent2 = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent2.putExtra("list", melist);
                    picintent2.putExtra("poistion", 1);
                    startActivity(picintent2);
                }
                break;
            case R.id.stealeachphotos_you_pic3:
                if (melist.size() > 2) {
                    Intent picintent3 = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent3.putExtra("list", melist);
                    picintent3.putExtra("poistion", 2);
                    startActivity(picintent3);
                }
                break;
            case R.id.stealeachphotos_me_pic1:
                if (yourlist.size() > 0) {
                    Intent picintent = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent.putExtra("list", yourlist);
                    picintent.putExtra("poistion", 0);
                    startActivity(picintent);
                }
                break;
            case R.id.stealeachphotos_me_pic2:
                if (yourlist.size() > 1) {
                    Intent picintent = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent.putExtra("list", yourlist);
                    picintent.putExtra("poistion", 1);
                    startActivity(picintent);
                } else {
                    Intent emptyintent = new Intent(StealEachPhotosActivity.this, StealPhotosDateActivity.class);
                    emptyintent.putExtra("beUserid", beUserid);
                    emptyintent.putExtra("lookcount", 1);
                    emptyintent.putExtra("yourPhotoStr", yourPhotoStr);
                    emptyintent.putExtra("yourunickname", stealeachphotos_you_name.getText().toString());
                    emptyintent.putExtra("yourunseenPhotoStr", yourunseenPhotoStr);
                    emptyintent.putExtra("stealPhotoInfoList", stealPhotoInfoList);
                    startActivity(emptyintent);
                }
                break;
            case R.id.stealeachphotos_me_pic3:
                if (yourlist.size() > 2) {
                    Intent picintent = new Intent(StealEachPhotosActivity.this, StealPhotoBigShowActivity.class);
                    picintent.putExtra("list", yourlist);
                    picintent.putExtra("poistion", 2);
                    startActivity(picintent);
                } else {
                    Intent emptyintent = new Intent(StealEachPhotosActivity.this, StealPhotosDateActivity.class);
                    emptyintent.putExtra("beUserid", beUserid);
                    emptyintent.putExtra("lookcount", 2);
                    emptyintent.putExtra("yourPhotoStr", yourPhotoStr);
                    emptyintent.putExtra("yourunickname", stealeachphotos_you_name.getText().toString());
                    emptyintent.putExtra("yourunseenPhotoStr", yourunseenPhotoStr);
                    emptyintent.putExtra("stealPhotoInfoList", stealPhotoInfoList);
                    startActivity(emptyintent);
                }
                break;
            case R.id.stealeachphotos_me_empty:
                Intent emptyintent = new Intent(StealEachPhotosActivity.this, StealPhotosDateActivity.class);
                emptyintent.putExtra("beUserid", beUserid);
                emptyintent.putExtra("yourunickname", stealeachphotos_you_name.getText().toString());
                emptyintent.putExtra("yourunseenPhotoStr", yourunseenPhotoStr);
                emptyintent.putExtra("stealPhotoInfoList", stealPhotoInfoList);
                startActivity(emptyintent);
                break;
        }
    }

    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionupdateStealEachPhotos:
                    initData();
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
    }
}
